home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK2.toast / Development Kits (Disc 2) / ScriptX / Code Samples / autofind / source / traveler.sx < prev   
Encoding:
Text File  |  1996-05-21  |  2.5 KB  |  95 lines  |  [TEXT/ttxt]

  1. --<<<-
  2. -- Filename: 
  3. --     traveler.sx
  4.  
  5. -- Other Files Required:
  6. --     None
  7.  
  8. -- Purpose:  
  9. --     Class definition for the Traveler class.
  10.  
  11. -- Specialized Classes:  
  12. --     Traveler
  13.  
  14. -- Instructions to User:
  15. --     Defines the Traveler class, which supports simple interpolation to any arbitrary
  16. --     destination. The Traveler has a clock, which tickles an interpolator when the
  17. --     Traveler is traveling.
  18. --
  19. --     Protocol:
  20. --     travel self destination duration    -- interpolate to location <destination> in duration ticks.
  21. --     leave                            -- interpolate using present destination and duration
  22.  
  23. -- Author:
  24. --     Steve Mayer
  25.  
  26. -- Class Traveler is an object which can travel to destination points
  27. -- in a specified duration. Uses an Interpolator to do the traveling.
  28.  
  29. in module Autofinder
  30.  
  31. class Traveler ()
  32. instance variables
  33.     travelClock
  34.     travelInterpolator
  35.     destination
  36.     duration
  37.     ready
  38. end
  39.  
  40. method init self {class Traveler} #rest args #key travelClock: (new Clock scale:12) \
  41.     travelInterpolator: (undefined) destination: duration: ->
  42. (
  43.     apply nextMethod self args
  44.     self.travelClock := travelClock
  45.     self.travelInterpolator := travelInterpolator
  46.     self.destination := destination
  47.     self.duration := duration
  48.     self.ready := false
  49.     return self
  50. )
  51.  
  52. method arrive self {class Traveler} ->
  53. (
  54.     self.travelClock.rate := 0
  55.     self.ready := false
  56.     self.travelInterpolator.space := undefined
  57.     self.travelInterpolator := undefined
  58. )
  59.  
  60. method travelPrepare self {class Traveler} ->
  61. (
  62.     self.travelInterpolator := new Interpolator space:self.presentedBy clock:self.travelClock
  63.     append self.travelInterpolator self
  64.     self.ready := true
  65. )
  66.  
  67. method leave self {class Traveler} ->
  68. (
  69.     if (not self.ready) do travelPrepare self
  70.     
  71.     -- Calculate destination time and set the destination of the Interpolator.
  72.     local destinationTime := self.travelClock.time + self.duration
  73.     setDestination self.travelInterpolator self.destination destinationTime
  74.     
  75.     -- Add a callback for the end of the trip, and start traveling.
  76.     -- remove any previous TimeCallbacks
  77.     local cb := chooseAll self.travelClock.callBacks (tcb dummy -> \
  78.          isAKindOf tcb TimeCallBack) undefined
  79.     forEach cb (item arg -> cancel item) undefined
  80.  
  81.     -- Add a callback for the end of the trip, and start traveling.
  82.     addTimeCallback self.travelClock arrive self #() destinationTime true
  83.     self.travelClock.rate := 1
  84. )
  85.  
  86. method travel self {class Traveler} destination duration ->
  87. (
  88.     if (not self.ready) do travelPrepare self
  89.     self.destination := destination
  90.     self.duration := duration
  91.     leave self
  92. )
  93. -->>>
  94. "Compiled traveler.sx"
  95.